def resize(b_image: bytes):
    """移除图像背景，自动处理超大尺寸图像"""
    from PIL import Image
    import io
    import json
    
    img = Image.open(io.BytesIO(b_image))
    # 获取原始图片格式
    original_format = img.format
    origin_w, origin_h = img.size
    
    # Check if resizing is needed
    if origin_h > 4096 or origin_w > 4096:
        # Calculate new dimensions while maintaining aspect ratio
        if origin_h > origin_w:
            new_h = 4096
            new_w = int((origin_w / origin_h) * new_h)
        else:
            new_w = 4096
            new_h = int((origin_h / origin_w) * new_w)
        
        # Resize the image
        img = img.resize((new_w, new_h), Image.Resampling.LANCZOS)
        
        # Convert back to bytes
        output_buffer = io.BytesIO()
        # 如果原始格式为None（可能是从字节流加载的图片），可以设置一个默认格式
        if original_format is None:
            original_format = 'PNG'  # 设置默认格式
        img.save(output_buffer, format=original_format)  # or keep original format
        b_image = output_buffer.getvalue()
    
    # Prepare the origin size JSON string
    origin_size = {
        'original_height': origin_h,
    'original_width': origin_w
    }
    origin_size_json_str = json.dumps(origin_size)
    
    return b_image, origin_size_json_str

